import {
	mapMutations
} from 'vuex'

export default {
	...mapMutations([
		'upUserInfo', 'upLastInformation'
	]),

	showCouponAmount() {
		this.$refs.couponPopup.open()
	},

	async addUser() {
		this.$refs.userPop.show();
	},
	// 选择好友
	selectFriend(friends) {
		this.mentionedFriends = friends; // 添加到提到的好友列表
		this.isFocus = true
		this.hideFriendList(); // 隐藏好友列表
	},
	delUser(index) {
		this.mentionedFriends.splice(index, 1)
	},

	// 输入事件监听
	onInputChange(event) {
		const value = event.detail.value;
		this.comment_content = value;
	},

	// 隐藏好友列表
	hideFriendList() {
		this.$refs.userPop.close();
	},
	scrollTo(selector) {
		uni.pageScrollTo({
			selector: "#" + selector, // 找到ID滚动到指定位置
			duration: 100 // 滚动动画的时长，默认300ms，单位 ms
		});
	},
	//展开收起
	isOpen() {
		this.isMore = !this.isMore
	},
	share() {
		// this.$refs.popup.open()
	},
	//打开评论弹窗
	//打开评论弹窗
	openComment(level_or_id, place) {
		if (level_or_id == 'level1') { //true为评论二级
			this.parentId = null
		} else {
			this.parentId = level_or_id
		}
		this.place = place
		this.$refs.send.open()


	},
	sendPopChange(e) {
		if (e.show) {
			setTimeout(() => {
				this.isFocus = true
			}, 100);
		} else {
			setTimeout(() => {
				this.isFocus = false
			}, 100);
		}
	},
	selectShop(item) {
		this.commentInformationId = item.id
	},
	handlerUpload() {
		let that = this
		that.$httpRequest.handlerUploadPic(res => {
			if (res.code === that.$httpConfig.SUCCESS_CODE) {
				this.comment_pic = res.data.filePath
				// this.page = 1
				// this.list = []
				// this.addInformationComment(2)
				this.uplaodShow = true
			}
		});
	},
	delCommentPic() {
		this.comment_pic = ''
		this.uplaodShow = false
	},
	// 领取优惠券 
	receiveCoupon() {
		let that = this
		that.$httpRequest.request({
			that: that,
			url: 'wechat/receiveCoupon',
			params: {
				couponId: that.detail.couponId
			},
			success: res => {
				if (res.code === that.$httpConfig.SUCCESS_CODE) {
					that.$toast.msg(res.message, {
						icon: 1
					})
					that.detail.receiveCoupon = true
				}
			}
		})
		this.$refs.couponPopup.close()
	},
	//评论后获取列表
	commentList() {
		let that = this
		that.$httpRequest.get({
			that: that,
			url: 'wechat/getInformationCommentList',
			params: {
				informationId: that.id,
				pageNo: that.page,
				pageSize: that.list_rows,
			},
			success: res => {
				if (res.code === that.$httpConfig.SUCCESS_CODE) {
					that.list = res.data.rows || []

				}

			}
		})
	},
	//获取指定信息的评论列表
	getInformationCommentList() {
		let that = this
		that.$httpRequest.get({
			that: that,
			url: 'wechat/getInformationCommentList',
			params: {
				informationId: that.id,
				pageNo: that.page,
				pageSize: that.list_rows,
			},
			success: res => {
				if (res.code === that.$httpConfig.SUCCESS_CODE) {
					if (that.page === 1) {
						that.list = res.data.rows || []
					} else {
						that.list = that.list.concat(res.data.rows)
					}
					that.page++
				}

			}
		})
	},
	delComment(id, indexParent, indexChild, isLevel, userId) {
		if (this.userInfo.id !== userId) {
			// this.$toast.msg('不能删除他人的评论', {
			// 	icon: 1
			// })
			return false
		}
		let that = this;
		uni.showActionSheet({
			itemList: ['删除该评论'],
			success: function(res) {
				if (res.tapIndex == 0) {
					that.$httpRequest.get({
						that: that,
						url: 'wechat/delInformationComment',
						params: {
							id: id
						},
						success: res => {
							if (res.code === that.$httpConfig.SUCCESS_CODE) {
								// that.page = 1
								// that.list = []
								// that.getInformationCommentList()
								if (!isLevel) { //是否有二级
									that.list.splice(indexParent, 1)
								} else {
									that.list[indexParent].comments.splice(indexChild, 1)
								}

							}
							// that.$toast.msg(res.message, {
							// 	icon: 1
							// })
						}
					})
				} else {


				}
				// console.log('选中了第' + (res.tapIndex + 1) + '个按钮');
			},
			fail: function(res) {
				console.log(res.errMsg);
			}
		});
		wx.vibrateShort()
	},
	getVisitInformation() {
		let that = this
		that.$httpRequest.get({
			that: that,
			url: 'wechat/visitInformation',
			params: {
				id: that.id,
			},
			success: res => {
				if (res.code === that.$httpConfig.SUCCESS_CODE) {


				}

			}
		})
	},
	//关注
	follow(id) {
		let that = this
		that.$httpRequest.request({
			that: that,
			url: 'wechat/follow',
			params: {
				followUserId: id
			},
			success: res => {
				if (res.code === that.$httpConfig.SUCCESS_CODE) {
					that.detail.follow = true
					// that.isLike = res.data.is_collect

				}
			}
		})
	},
	unfollow(id) {
		let that = this
		that.$toast.confirm({
			content: '确定取消关注？',
			confirmText: '确定',
			callback: result => {
				if (result) {
					that.$httpRequest.request({
						that: that,
						url: 'wechat/unfollow',
						params: {
							followUserId: id
						},
						success: res => {
							if (res.code === that.$httpConfig.SUCCESS_CODE) {
								that.detail.follow = false
								// that.isLike = res.data.is_collect

							}
						}
					})
				} else {

				}
			}
		})
	},
	//收藏信息
	collectInformation() {
		let that = this
		that.$httpRequest.request({
			that: that,
			url: 'wechat/collectInformation',
			params: {
				informationId: that.id
			},
			success: res => {
				if (res.code === that.$httpConfig.SUCCESS_CODE) {
					++that.detail.collectCount
					that.detail.collect = true

				}
				// that.$toast.msg(res.message, {
				// 	icon: 1
				// })
			}
		})
	},
	//取消收藏信息
	cancelCollectInformation() {
		let that = this
		that.$httpRequest.request({
			that: that,
			url: 'wechat/cancelCollectInformation',
			params: {
				informationId: that.id
			},
			success: res => {
				if (res.code === that.$httpConfig.SUCCESS_CODE) {
					--that.detail.collectCount
					that.detail.collect = false

				}
				// that.$toast.msg(res.message, {
				// 	icon: 1
				// })
			}
		})
	},
	//点赞信息
	favoriteInformation() {
		let that = this
		that.$httpRequest.request({
			that: that,
			url: 'wechat/favoriteInformation',
			params: {
				informationId: that.id
			},
			success: res => {
				if (res.code === that.$httpConfig.SUCCESS_CODE) {
					++that.detail.favoriteCount
					that.detail.favorite = true
				}
				// that.$toast.msg(res.message, {
				// 	icon: 1
				// })
			}
		})
	},
	//取消点赞信息
	cancelFavoriteInformation() {
		let that = this
		that.$httpRequest.request({
			that: that,
			url: 'wechat/cancelFavoriteInformation',
			params: {
				informationId: that.id
			},
			success: res => {
				if (res.code === that.$httpConfig.SUCCESS_CODE) {
					--that.detail.favoriteCount
					that.detail.favorite = false
				}
				// that.$toast.msg(res.message, {
				// 	icon: 1
				// })
			}
		})
	},
	//点赞评论
	favoriteComment(item) {
		let that = this
		that.$httpRequest.request({
			that: that,
			url: 'wechat/favoriteComment',
			params: {
				informationId: that.id,
				commentId: item.id
			},
			success: res => {
				if (res.code === that.$httpConfig.SUCCESS_CODE) {

					item.isFavorite = true
				}

			}
		}, false)
	},
	//取消点赞评论
	cancelFavoriteComment(item) {
		let that = this
		that.$httpRequest.request({
			that: that,
			url: 'wechat/cancelFavoriteComment',
			params: {
				informationId: that.id,
				commentId: item.id
			},
			success: res => {
				if (res.code === that.$httpConfig.SUCCESS_CODE) {
					item.isFavorite = false
				}

			}
		}, false)
	},
	//键盘回车
	sumbit_comment() {
		if (this.shopShow) {
			this.addInformationComment(3)
			this.shopShow = !this.shopShow
		} else if (this.uplaodShow) {
			this.addInformationComment(2)
			this.uplaodShow = !this.uplaodShow
		} else {
			this.addInformationComment(1)
		}

	},
	//新增信息评论
	addInformationComment(type) {
		let that = this
		let params;
		if (type == 1 && that.parentId) {
			params = {
				informationId: that.id,
				type,
				content: that.comment_content,
				parentId: that.parentId,
				atUsers: that.mentionedFriends.map((item) => {
					return item.id
				}).join(",")

			}
		} else if (type == 1 && !that.parentId) {
			params = {
				informationId: that.id,
				type,
				content: that.comment_content,
				atUsers: that.mentionedFriends.map((item) => {
					return item.id
				}).join(",")
			}
		} else if (type == 2 && that.parentId) {
			params = {
				informationId: that.id,
				type,
				content: that.comment_content,
				parentId: that.parentId,
				img: that.comment_pic

			}
		} else if (type == 2 && !that.parentId) {
			params = {
				informationId: that.id,
				content: that.comment_content,
				type,
				img: that.comment_pic

			}
		} else if (type == 3 && that.parentId) {

			params = {
				informationId: that.id,
				type,
				content: that.comment_content,
				parentId: that.parentId,
				commentInformationId: that.commentInformationId
			}
		} else if (type == 3 && !that.parentId) {
			params = {
				informationId: that.id,
				type,
				content: that.comment_content,
				commentInformationId: that.commentInformationId

			}
		}
		if (!params.img && !params.content && !that.commentInformationId) {
			return false
		}

		that.$httpRequest.request({
			that: that,
			url: 'wechat/addInformationComment',
			params: params,
			success: res => {
				if (res.code === that.$httpConfig.SUCCESS_CODE) {
					// console.log("评论完成")
					that.page = 1
					that.list = []
					that.commentList()
					that.comment_content = ''
					that.mentionedFriends = []
					that.$refs.send.close()
					that.isFocus = false
				}
				// that.$toast.msg(res.message, {
				// 	icon: 1
				// })
			}
		})
	},
	//页面数据
	getData(id) {
		let that = this
		that.$httpRequest.request({
			that: that,
			method: 'get',
			url: 'wechat/getInformationDetail',
			params: {
				id: id
			},
			success: res => {
				if (res.code === that.$httpConfig.SUCCESS_CODE) {
					that.detail = res.data;
					that.upLastInformation(res.data);
					if (that.detail.filesList.length <= 1) { //轮播图的点点
						that.dots = false
					}
					//高度自适应
					if (that.detail.filesList[0].fileSuffix == 'png' || that.detail.filesList[0]
						.fileSuffix == 'jpg' || that.detail.filesList[0].fileSuffix == 'jpge') {
						const getSystemInfo = uni.getSystemInfoSync()
						const {
							windowWidth
						} = getSystemInfo; //屏幕可视宽高
						// console.log("屏幕宽度", windowWidth)
						that.$nextTick(function() {
							setTimeout(() => {
								const query = uni.createSelectorQuery();
								query.select('.cover_img').boundingClientRect(data => {
									// that.swiperHeight = data.height;
									// console.log(data, "图片高度");
									if (data.height > 450) {
										that.swiperHeight = 450
									} else {
										that.swiperHeight = data.height
									}
								}).exec();

							}, 100)

						})

					} else {
						that.swiperHeight = 550 //图片之外的高度
					}
					// 自动播放视频
					that.detail.filesList.forEach((item, index) => {
						if (item.fileSuffix !== 'png' && item.fileSuffix !== 'jpg' && item
							.fileSuffix !== 'jpge' && index == 0) {
							this.currentVideo = 'video' + index
							//获取 video 上下文 videoContext 对象
							this.videoContext = uni.createVideoContext(this.currentVideo);
							uni.createVideoContext(this.currentVideo, this).play();
						}
					})

				} else if (res.code == 403) {
					console.log(res.code, "403删除作品")
					that.block = false
				}
			}
		})
	},
	toggle(index) {
		this.control = true
	},
	//收藏信息
	collectInformation() {
		let that = this
		that.$httpRequest.request({
			that: that,
			url: 'wechat/collectInformation',
			params: {
				informationId: that.id
			},
			success: res => {
				if (res.code === that.$httpConfig.SUCCESS_CODE) {
					++that.detail.collectCount
					that.detail.collect = true

				}
				// that.$toast.msg(res.message, {
				// 	icon: 1
				// })
			}
		})
	},
	//取消收藏信息
	cancelCollectInformation() {
		let that = this
		that.$httpRequest.request({
			that: that,
			url: 'wechat/cancelCollectInformation',
			params: {
				informationId: that.id
			},
			success: res => {
				if (res.code === that.$httpConfig.SUCCESS_CODE) {
					--that.detail.collectCount
					that.detail.collect = false

				}
				// that.$toast.msg(res.message, {
				// 	icon: 1
				// })
			}
		})
	},
	//点赞信息
	favoriteInformation() {
		let that = this
		that.$httpRequest.request({
			that: that,
			url: 'wechat/favoriteInformation',
			params: {
				informationId: that.id
			},
			success: res => {
				if (res.code === that.$httpConfig.SUCCESS_CODE) {
					++that.detail.favoriteCount
					that.detail.favorite = true
				}
				// that.$toast.msg(res.message, {
				// 	icon: 1
				// })
			}
		})
	},
	//取消点赞信息
	cancelFavoriteInformation() {
		let that = this
		that.$httpRequest.request({
			that: that,
			url: 'wechat/cancelFavoriteInformation',
			params: {
				informationId: that.id
			},
			success: res => {
				if (res.code === that.$httpConfig.SUCCESS_CODE) {
					--that.detail.favoriteCount
					that.detail.favorite = false
				}
				// that.$toast.msg(res.message, {
				// 	icon: 1
				// })
			}
		})
	},
	scrollLower() {
		this.getMyInformationList();
	},
	getMyInformationList() {
		let that = this
		that.$httpRequest.get({
			that: that,
			url: 'wechat/getMyInformationList',
			params: {
				type: 2,
				pageNo: that.shopPage,
				pageSize: that.list_rows,
			},
			success: res => {
				that.loading = false
				if (res.code === that.$httpConfig.SUCCESS_CODE) {
					if (that.shopPage === 1) {
						that.shopList = res.data.rows
					} else {
						that.shopList = that.shopList.concat(res.data.rows)
					}
					that.shopPage++
				}
			}
		})
	},
	toBuy(id) {
		uni.navigateTo({
			url: '/pages/my/buy?id=' + id
		})
	},
	//切换暂停视频
	changeVideo(val) {
		let _this = this;
		let currentId = 'video' + val.detail.current; // 获取当前视频id
		//播放选中视频
		uni.createVideoContext(currentId, _this).play();
		// 获取视频列表
		let trailer = _this.detail.filesList;
		trailer.forEach((item, index) => { // 获取json对象并遍历, 停止非当前视频
			if (item.fileSuffix !== 'png' && item.fileSuffix !== 'jpg' && item.fileSuffix !== 'jpge') {
				let temp = 'video' + index;
				// console.log(temp, currentId)
				if (temp != currentId) {
					// 暂停其余视频
					uni.createVideoContext(temp, _this).pause(); //暂停视频播放事件
				}
			}

		})
	},
	fullscreenchange(el) {
		// el.detail.fullScreen
		let _this = this
		if (el.detail.fullScreen) {
			_this.objectFit = 'contain'
		} else {
			_this.objectFit = 'cover'
		}
	},
	openVideo(index) {
		this.currentVideo = 'video' + index
		//获取 video 上下文 videoContext 对象
		this.videoContext = uni.createVideoContext(this.currentVideo);
		// uni.createVideoContext(this.currentVideo,this).play(); 

	},
	_previewImage(imgArr, index) {
		//预览图片
		const imgs = imgArr.map((item) => {
			return item.filePath
		})
		uni.previewImage({
			urls: imgs,
			current: imgs[index]
		});
	},
	//加载图片完成
	imgloadComplete(e) {
		let that = this
		const getSystemInfo = uni.getSystemInfoSync()
		const {
			windowWidth
		} = getSystemInfo; //屏幕可视宽高
		const query = uni.createSelectorQuery();
		query.select('.cover_img').boundingClientRect(data => {
			if (data.height > 450) {
				that.swiperHeight = 450
			} else {
				that.swiperHeight = data.height
			}
		}).exec();
	},
	// 预览图片
	previewImage(url, index) {
		uni.previewImage({
			current: index, // 当前显示图片的链接/索引值
			urls: [url], // 需要预览的图片链接列表，photoList要求必须是数组
			loop: true // 是否可循环预览
		});
	},
	//展开评论
	moreComment(index) {
		this.list[index].isShow = !this.list[index].isShow
	}


}